package com.hyzx.qbasic.admin.controller;

import com.hyzx.qbasic.admin.model.constant.SessionKeyConst;
import com.hyzx.qbasic.admin.common.security.ShiroUtils;
import com.hyzx.qbasic.admin.common.exception.IncorrectCaptchaException;
import com.hyzx.qbasic.admin.model.annotation.DbLogIgnore;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.common.util.VerifyCodeUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * Created by 黄江华 on 2017/9/7
 */

@ApiIgnore
@Controller
public class LoginController extends BaseController {

    @Autowired
    private HttpServletRequest request;

    @SysLog("用户登录页")
    @GetMapping("/login")
    public String login() {
        ShiroUtils.logout();

        return "login";
    }

    @SysLog("用户登录失败页")
    @PostMapping("/login")
    public String login(Model model) {
        String error;
        String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");

        if (IncorrectCaptchaException.class.getName().equals(exceptionClassName))
            error = "验证码不正确";
        else if (UnknownAccountException.class.getName().equals(exceptionClassName))
            error = "用户名/密码错误";
        else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName))
            error = "用户名/密码错误";
        else if (LockedAccountException.class.getName().equals(exceptionClassName))
            error = "账号已锁定";
        else
            error = "账号验证失败";

        model.addAttribute("error", error);
        return "login";
    }

    /**
     * 生成登录验证码
     *
     * @return
     */
    @SysLog("获取登录验证码")
    @GetMapping("/captcha.jpg")
    public void getCaptcha(HttpServletResponse response) {
        //设置页面不缓存
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        //设置输出的内容的类型为JPEG图像
        response.setContentType("image/jpeg");

        String verifyCode = VerifyCodeUtils.generateTextCode(VerifyCodeUtils.TYPE_NUM_UPPER, 5, null);
        ShiroUtils.setSessionAttribute(SessionKeyConst.CAPTCHA_SESSION_KEY, verifyCode);

        try {
            BufferedImage bufferedImage = VerifyCodeUtils.generateImageCode(verifyCode, 130, 34, 3, true, Color.WHITE, Color.BLACK, null, null);
            //写给浏览器
            ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
        } catch (IOException ex) {
            logger.error("生成验证码异常", ex);
        }
    }

    @DbLogIgnore
    @SysLog("检查用户是否会话过期")
    @GetMapping("/checkSessionTimeout")
    @ResponseBody
    public String checkSessionTimeout() {
        return "ok";
    }
}
